package com.umeox.watch.moto.sos.task;

import android.content.Context;

import com.umeox.moto.common.log.Logger;
import com.umeox.moto.common.recorder.SilentRecord;
import com.umeox.moto.common.utils.FileUtils;


public class RecordTask implements SilentRecord.RecordListener {
    private static final int MILLIS_IN_RECORDING = 30 * 1000;
    private static Context sContext;
    private SilentRecord mMediaRecorder;
    private RecordListener onTickListener;

    private RecordTask(String dir, int maxDuration) {
        mMediaRecorder = new SilentRecord(sContext, dir, maxDuration, this);
    }

    private RecordTask(String dir, int maxDuration, int minDuration) {
        mMediaRecorder = new SilentRecord(dir, maxDuration, minDuration, this);
    }

    public static RecordTask createTask() {
        return createTask(FileUtils.getVoiceCaheDir());
    }

    public static RecordTask createTask(int maxDuration, Context context) {
        sContext = context;

        return createTask(FileUtils.getVoiceCaheDir(), maxDuration);
    }

    public static RecordTask createTask(String dir) {
        return createTask(dir, MILLIS_IN_RECORDING);
    }

    public static RecordTask createTask(String dir, int maxDuration) {
        return new RecordTask(dir, maxDuration);
    }

    public static RecordTask createTask(String dir, int maxDuration, int minDuration) {
        return new RecordTask(dir, maxDuration, minDuration);
    }

    public void setOnTickListener(RecordListener onTickListener) {
        this.onTickListener = onTickListener;
    }

    public void start() {
        mMediaRecorder.startRecord();
    }

    public void cancel() {
        mMediaRecorder.cancelRecord();
    }

    public void release() {
        mMediaRecorder.release();
    }

    @Override
    public void onRecordStateChanged(SilentRecord.RecorderState oldState, SilentRecord.RecorderState newState) {
        if (newState == SilentRecord.RecorderState.STATE_IDLE) {
            Logger.i("Record state change to IDLE");
        } else if (newState == SilentRecord.RecorderState.STATE_RECORDING) {
            Logger.i("Record state change to RECORDING");
            if (this.onTickListener != null) {
                this.onTickListener.onStart();
            }
        } else if (newState == SilentRecord.RecorderState.STATE_RECORD_END) {
            String filePath = mMediaRecorder.getRecordPath();
            Logger.w("录音完成->" + filePath);
            if (this.onTickListener != null) {
                this.onTickListener.onEnd(filePath);
            }
        } else if (newState == SilentRecord.RecorderState.STATE_CANCEL) {
            Logger.w("Cancel Record By User");
        } else if (newState == SilentRecord.RecorderState.STATE_ERROR) {
            Logger.w("Record state change to ERROR");
            if (this.onTickListener != null) {
                this.onTickListener.onError();
            }
        }
    }

    @Override
    public void onRecordMicStatusUpdate(int db) {
    }

    @Override
    public void onTick(long millisUntilFinished) {
        if (this.onTickListener != null) {
            this.onTickListener.onTick(millisUntilFinished);
        }
    }

    public interface RecordListener {
        void onStart();

        void onError();

        void onEnd(String filePath);

        void onTick(long millisUntilFinished);
    }
}
